﻿
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Product.API.Applcation.Command.ProductType;
using Product.API.Applcation.Command.ProductTypeInfo;
using Product.API.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Product.API.Controllers
{
    /// <summary>
    /// 产品分类写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]

    public class ProductController : ControllerBase
    {
        protected readonly IMediator mediator;

        public ProductController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 产品分类添加
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductTypeAdd(ProductCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.AddName = name;
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 产品分类删除
        /// </summary>
        /// <param name="dd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductDelete(ProductDeleteCommand dd)
        {

            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                dd.U_Name = name;
                result.Data = await mediator.Send(dd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 产品分类修改
        /// </summary>
        /// <param name="dd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductUpdate(ProductUpdateCommand ud)
        {

            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                ud.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                ud.UpdName = name;
                result.Data = await mediator.Send(ud);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
